package net.caixiaomi.info.ui.football;

import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.view.View;

import net.caixiaomi.info.R;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.quickadapter.BaseViewHolder;
import net.caixiaomi.info.base.quickadapter.entity.MultiItemEntity;
import net.caixiaomi.info.helper.AppHelper;
import net.caixiaomi.info.model.ElementCellModel;
import net.caixiaomi.info.model.MatchPlaysList;
import net.caixiaomi.info.model.PlayFootballItem;
import net.caixiaomi.info.util.Constants;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 胜平负 / 让球胜平负
 */

public class TakeOrderAllGoalsProxy extends TakeOrderItemViewProxy<MultiItemEntity, BaseViewHolder> {
    private FootballTakeOrderAdapter mAdapter;

    public TakeOrderAllGoalsProxy(FootballTakeOrderAdapter adapter) {
        super(adapter);
        mAdapter = adapter;
    }

    @Override
    public void bindView(BaseViewHolder helper, MultiItemEntity item) {
        try {
            StringBuilder stringBuilder = new StringBuilder();
            PlayFootballItem model = (PlayFootballItem) item;
            MatchPlaysList matchPlaysList = model.getMatchPlays().get(0);
            helper.setVisible(R.id.action, TextUtils.equals(matchPlaysList.getSingle(), "1"));

            if (!TextUtils.isEmpty(model.getHomeTeamRank())) {
                stringBuilder.append("[").append(model.getHomeTeamRank()).append("]");
            }
            stringBuilder.append(model.getHomeTeamAbbr());
            helper.setText(R.id.host, stringBuilder);

            stringBuilder.setLength(0);
            if (!TextUtils.isEmpty(model.getVisitingTeamRank())) {
                stringBuilder.append("[").append(model.getVisitingTeamRank()).append("]");
            }
            stringBuilder.append(model.getVisitingTeamAbbr());
            helper.setText(R.id.guest, stringBuilder);
            stringBuilder.setLength(0);

            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            String time = sdf.format(new Date(Long.parseLong(model.getBetEndTime()) * 1000));
            stringBuilder.append(model.getLeagueAddr()).append(" ").append(model.getChangci()).append(" ").append(CommonApp.getInstance().getString(R.string.C_END_TIME)).append(time);
            helper.setText(R.id.text, stringBuilder);
            stringBuilder.setLength(0);

            boolean hasSelected = false;
            List<ElementCellModel> matchCells = matchPlaysList.getMatchCells();
            for (int index = 0; index < matchCells.size(); index++) {
                stringBuilder.setLength(0);
                ElementCellModel elementCellModel = matchCells.get(index);
                elementCellModel.setPlayType(matchPlaysList.getPlayType());
                stringBuilder.append(elementCellModel.getCellName()).append(" ").append(elementCellModel.getCellOdds());
                SpannableString spannableString = new SpannableString(stringBuilder);
                spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), elementCellModel.isSelected() ? R.color.white : R.color.third_text)), stringBuilder.length() - elementCellModel.getCellOdds().length(), stringBuilder.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(new RelativeSizeSpan(0.8f), stringBuilder.length() - elementCellModel.getCellOdds().length(), stringBuilder.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
                int id = AppHelper.getResourceId("button" + index);
                if (id != -1) {
                    helper.setText(id, spannableString);
                    helper.setTag(id, R.id.item_model, model);
                    helper.setTag(id, R.id.item_cell, elementCellModel);

                    helper.getView(id).setSelected(elementCellModel.isSelected());
                    if (elementCellModel.isSelected()) {
                        hasSelected = true;
                    }
                    helper.setOnClickListener(id, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ElementCellModel tag = (ElementCellModel) v.getTag(R.id.item_cell);
                            PlayFootballItem tagModel = (PlayFootballItem) v.getTag(R.id.item_model);
                            if (mAdapter.getListener() != null) {
                                if (mAdapter.getListener().canChange(tagModel)) {
                                    tag.setSelected(!tag.isSelected());
                                    if (tagModel.getSelectCell() == null) {
                                        tagModel.setSelectCell(new ArrayList<ElementCellModel>());
                                    }
                                    if (tag.isSelected()) {
                                        tagModel.getSelectCell().add(tag);
                                    } else {
                                        tagModel.getSelectCell().remove(tag);
                                    }
                                    mAdapter.getListener().onItemChangeListener(tagModel);
                                }
                            }
                        }
                    });
                }
            }
            helper.setGone(R.id.btn_delete, true);
            helper.addOnClickListener(R.id.btn_delete);
            helper.setGone(R.id.btn_bold, true);
            helper.addOnClickListener(R.id.btn_bold);
            helper.setTextColor(R.id.btn_bold, ContextCompat.getColor(CommonApp.getInstance(), R.color.primary_text));

            if (hasSelected) {
                helper.getView(R.id.btn_bold).setEnabled(true);
                if (model.isBlod()) {
                    helper.getView(R.id.btn_bold).setSelected(true);
                    helper.setTextColor(R.id.btn_bold, ContextCompat.getColor(CommonApp.getInstance(), R.color.orange_primary));
                } else {
                    helper.getView(R.id.btn_bold).setSelected(false);
                    if (mAdapter.getMaxDan() > 0) {
                        helper.getView(R.id.btn_bold).setEnabled(true);
                    } else {
                        helper.setTextColor(R.id.btn_bold, ContextCompat.getColor(CommonApp.getInstance(), R.color.light_gray));
                        helper.getView(R.id.btn_bold).setEnabled(false);
                    }
                }
            } else {
                helper.getView(R.id.btn_bold).setEnabled(false);
                helper.setTextColor(R.id.btn_bold, ContextCompat.getColor(CommonApp.getInstance(), R.color.light_gray));
            }

            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) helper.itemView.getLayoutParams();
            layoutParams.topMargin = (int) CommonApp.getInstance().getResources().getDimension(R.dimen.padding_8);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void addItemType() {
        mAdapter.addItemType(Constants.ITEM_VIEW_ITEM, R.layout.item_take_order_all_goals_proxy);
    }
}
